#include"network.h"

void geterrno()
{
    std::cout<<"请输入端口!"<<std::endl;
}

int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        geterrno();
        exit(LOG_LEVEL_ERRNO);
    }

    //创建套接字
    int token = socket(AF_INET, SOCK_STREAM, 0);
    if(token==-1)
    {
        log_message(LOG_LEVEL_ERROR,__FILE__,__LINE__,"错误码：%d,错误信息：%s",errno,strerror(errno));
        exit(LOG_LEVEL_SOCKET);
    }
    else
    {
        log_message(LOG_LEVEL_ERROR,__FILE__,__LINE__,"socket is successful");
    }  

    //客户端发送请求
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family=AF_INET;
    inet_pton(AF_INET, "120.48.75.223", &addr.sin_addr.s_addr);
    addr.sin_port=htons(atoi(argv[1]));

    socklen_t ip_addr=sizeof(addr);
    int can = connect(token,(const sockaddr*)&addr,ip_addr);
    if(can==-1)
    {
        log_message(LOG_LEVEL_ERROR,__FILE__,__LINE__,"错误码：%d,错误信息：%s",errno,strerror(errno));
        exit(LOG_LEVEL_CONNECT);
    }

    char buffer[1024]={0};
    while(1)
    {
        memset(buffer, 0, sizeof(buffer));
        //发送数据
        std::cout<<"请输入发送内容........."<<std::endl;
        getchar();
        scanf("%s", buffer);
        write(token,buffer,sizeof(buffer)-1);
    }
    close(token);

    return 0;
}